package com.ys.controller.api.purchase;

import com.ys.config.MenuId;
import com.ys.entity.purchase.TcgPurchaseInStorage;
import com.ys.entity.sys.YsCol;
import com.ys.service.purchase.TcgPurchaseInStorageService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 采购入库
 */
@Controller
@RequestMapping("/api/inter/tcgPurchaseInStorage")
public class ApiInterTcgPurchaseInStorageController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcgPurchaseInStorageController.class);


    @Autowired
    private TcgPurchaseInStorageService tcgPurchaseInStorageService;

    //菜单id
    private Long menuId = MenuId.TCG_PurchaseInStorage;

    @Autowired
    private CommonApiService<TcgPurchaseInStorage> commonApiService;

    //权限字符
    private final String PERMISSIONS_VIEW = "tcgPurchaseInStorage:view";
    private final String PERMISSIONS_EDIT = "tcgPurchaseInStorage:edit";

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @param cond
     * @return
     */
    public TcgPurchaseInStorage setSeachSql(TcgPurchaseInStorage cond){
        cond.addLeftJoin("TB_CompanyInfo D ON D.Vid = A.CompanyVid")//购买方公司
                .addField("D.Vname companyName")
            .addLeftJoin("TCW_PartnerBankInfo C on C.id = A.PartnerBankInfoId")//供应方公司
                .addField("C.PartnerName partnerBankInfoName")
            .addLeftJoin("THR_OperateCompanyApply TOCA ON TOCA.Vgsbm = A.Vgsbm")
                .addField("TOCA.Vgsjc AS vgsjc")//运营公司简称
                .addField("CASE WHEN A.StorageStatus = 0  THEN null ELSE A.updateTime END  updateTime1")//未入库就隐藏审核时间
            .addLeftJoin("TCC_Location CL ON CL.Chw = A.Chw")//货位
            .addLeftJoin("TCC_WarehouseArea CWA ON CWA.Ckq = CL.Ckq")//库区
            .addLeftJoin("TCC_Warehouse CW ON CW.Cck = CWA.Cck")//仓库
                .addField("CW.vck,CWA.vkq,CL.vhw")
        ;
        cond = commonApiService.dataAuth(menuId, cond);
        return  cond;
    }

    /**
     * 查询
     */
    @RequiresPermissions(PERMISSIONS_VIEW)
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TcgPurchaseInStorage cond, String conditions
    ){
        try {
            cond = setSeachSql(cond);

            LayuiTableVoJA setList = commonApiService.searchLay(cond);

            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /**
     * 采购入库
     */
    @RequiresPermissions(PERMISSIONS_EDIT)
    @RequestMapping("/updateRk")
    @ResponseBody
    public R updateRk(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //采购入库
            return tcgPurchaseInStorageService.updateRk(params,menuId);
        } catch (Exception e) {
            logger.error("采购入库出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 入库审核
     */
    @RequiresPermissions(PERMISSIONS_EDIT)
    @RequestMapping("/updateSh")
    @ResponseBody
    public R updateSh(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //采购入库
            return tcgPurchaseInStorageService.updateSh(params,menuId);
        } catch (Exception e) {
            logger.error("入库审核出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }


    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcgPurchaseInStorage cond, String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);

            cond = commonApiService.dataAuth(menuId, cond);
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(menuId)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();
            //处理数据
            ExportExcelUtils<TcgPurchaseInStorage> util = new ExportExcelUtils<>(new TcgPurchaseInStorage());
            String toFilePath = util.exportExcel(listData, listField, "采购入库报表");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }
}
